package com.yantong.controller.aftersale;

import com.yantong.annotation.Log;
import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.WorkCause;
import com.yantong.service.WorkCauseService;
import com.yantong.service.WorkClassService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 工单原因控制类
 * @author kanghaixin
 * @date 2018/9/3.14:06
 */
@Controller
@RequestMapping("/sales/orderCause")
public class WorkCauseController extends BaseController {

    private static final String prefix = "workorder/cause";
    @Autowired
    private WorkClassService workClassService;
    @Autowired
    private WorkCauseService workCauseService;


    /**
     * 工单原因列表
     * @param model
     * @return
     */
    @RequiresPermissions("sales:orderCause:orderCause")
    @GetMapping()
    public String orderCause(Model model) {
        model.addAttribute("orderTypes", workClassService.listAllTypes());
        return prefix + "/orderCause";
    }

    /**
     * 工单原因列表
     * @param typeId
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    @RequiresPermissions("sales:orderCause:orderCause")
    public PageUtils<WorkCause> listWorkCause(Long typeId, int offset, int limit) {
        return workCauseService.listWorkCause(typeId, offset, limit);
    }


    /**
     * 工单原因新增页面
     * @param model
     * @return
     */
    @RequiresPermissions("sales:orderCause:add")
    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("orderTypes", workClassService.listAllTypes());
        return prefix +"/add";
    }

    /**
     * 工单原因新增
     * @param workCause
     * @return
     */
    @Log(module = "工单原因管理", operation = "新增原因")
    @ResponseBody
    @RequiresPermissions("sales:orderCause:add")
    @PostMapping("/save")
    public ResultVo save(WorkCause workCause) {
        if (workCause == null) {
            return ResultVo.error();
        }
        if (workCauseService.save(workCause)< 1){
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 工单原因编辑页面
     * @param model
     * @return
     */
    @RequiresPermissions("sales:orderCause:edit")
    @GetMapping("/edit/{causeId}")
    public String edit(Model model, @PathVariable Long causeId) {
        model.addAttribute("orderTypes", workClassService.listAllTypes());
        model.addAttribute("workCause", workCauseService.getByKey(causeId));
        return prefix +"/edit";
    }

    /**
     * 工单原因编辑
     * @param workCause
     * @return
     */
    @Log(module = "工单原因管理", operation = "编辑原因")
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("sales:orderCause:edit")
    public ResultVo update(WorkCause workCause) {
        if (workCause == null || workCause.getId() == null) {
            return ResultVo.error();
        }
        if (workCauseService.update(workCause)< 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 工单原因删除
     * @param causeId
     * @return
     */
    @Log(module = "工单原因管理", operation = "删除原因")
    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("sales:orderCause:remove")
    public ResultVo remove(Long causeId) {
        if (causeId == null) {
            return ResultVo.error();
        }
        WorkCause workCause = new WorkCause();
        workCause.setIsdelete(1);
        workCause.setId(causeId);
        if (workCauseService.update(workCause)< 1){
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 获取工单类型下的所有工单原因
     * @param type
     * @return
     */
    @ResponseBody
    @PostMapping("/getAllWorkCause")
    public ResultVo getAllWorkCause(Long type) {
        if (type == null) {
            return ResultVo.error();
        }
        return ResultVo.ok().put("causeList", workCauseService.listAllCause(type));
    }
}
